//
//  PTPrivacyPolicyView.swift
//  Donpo
//
//  Created by benwang on 2020/1/15.
//  Copyright © 2020 benco. All rights reserved.
//

import UIKit
import WebKit

/// 便捷访问
extension PTSheetHandle.Content {
    /// 隐私协议
    static var policy: PTSheetHandle.Content {
        let view = PTPrivacyPolicyView()
        return PTSheetHandle.Content(sheet: view)
    }
}

/// 可配置的弹框协议
extension PTPrivacyPolicyView: PTSheetConfigurable {
    var handle: PTSheetHandle.Handle? {
        get {
            return completeHandle
        }
        set {
            completeHandle = newValue
        }
    }

    func shouldUpdateLayoutConstraints() {

        self.snp.remakeConstraints { (make) in
            make.top.equalToSuperview().offset(50).priority(.high)
            make.left.equalToSuperview().offset(50).priority(.high)
            make.right.equalToSuperview().offset(-50).priority(.high)
            make.bottom.equalToSuperview().offset(-50).priority(.high)
            make.width.lessThanOrEqualTo(430)
            make.height.lessThanOrEqualTo(600)
            make.center.equalToSuperview()
        }
    }
}

/// 隐私协议
class PTPrivacyPolicyView: UIView {

    private var titleLabel: UILabel = {
        let label = UILabel(frame: .zero)
        label.textAlignment = .center
        label.font = UIFont.boldSystemFont(ofSize: 18)
        label.text = "温馨提示"
        return label
    }()

    private var webView: WKWebView = {
        return WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
    }()

    private var line: UIView = {
        let view = UIView(frame: .zero)
        view.backgroundColor = .groupTableViewBackground
        return view
    }()

    private let agreementButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("我知道了", for: .normal)
        button.setTitleColor(.systemOrange, for: .normal)
        button.addTarget(self, action: #selector(agreementClick), for: .touchUpInside)
        return button
    }()

    private var completeHandle: PTSheetHandle.Handle?

    override init(frame: CGRect) {
        super.init(frame: .zero)

        self.backgroundColor = .white
        self.layer.cornerRadius = 8
        self.layer.masksToBounds = true

        self.addSubview(titleLabel)
        self.addSubview(webView)
        self.addSubview(line)
        self.addSubview(agreementButton)

        renderContentView()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func renderContentView() {

        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
        }
        webView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.bottom.equalTo(line.snp.top)
            make.left.equalToSuperview().offset(8)
            make.right.equalToSuperview().offset(-8)
        }
        line.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalTo(webView.snp.bottom)
            make.height.equalTo(1)
            make.bottom.equalTo(agreementButton.snp.top)
        }
        agreementButton.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(50)
            make.bottom.equalToSuperview()
        }

        webView.load(URLRequest(url:URL(string: "http://www.rzds.net/zthd/privacypolicy.html")!))
    }

    @objc func agreementClick() {
        completeHandle?(.ensure)
    }
}
